<?php
namespace Framework;
/**
 * 框架入口文件
 */
class Framework{
	
	public $application;
	private $appPath;
	
	function __construct( $app_path ){
		
		$this->appPath = $app_path;
		
		ob_start();
		
		try {
			
			$this ->init();
			
		} catch (\Exception $e) {
			
			echo '<pre>';
			print_r($e);
			echo '</pre>';
			
		}
		
		ob_end_flush();
		
	}
	
	/**
	 * 初始化应用程序设置
	 * 
	 * @return void
	 */
	private function init(){
		
		set_error_handler('Framework\Framework::throwErrorException');
		
		error_reporting(-1);
		
		$GLOBALS['Framework'] = $this;
		
		session_start();
		
		require_once 'Library/Core/Application.class.php';
		require_once 'Library/Core/Form.class.php';
		require_once 'Library/Core/Entity.class.php';
		require_once 'Library/Component/FileEntity.class.php';
		require_once 'Library/Core/Datalist.class.php';
		
		$this ->application = new Library\Core\Application( $this->appPath );
		$this->application->run(); // 不让它自动运行的原因是，为了让Application内的对象能通过全局变量访问$GLOBALS['Framework']->application
		
	}
	
	/**
	 * 把PHP错误转换成异常对象抛出
	 * 
	 * @param unknown_type $errno
	 * @param unknown_type $errstr
	 * @param unknown_type $errfile
	 * @param unknown_type $errline
	 * @throws ErrorException
	 */
	public static function throwErrorException( $errno, $errstr, $errfile, $errline ){
		
		throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
		
	}
	
}